﻿using System;
using System.Drawing;
using FBEntities.FBObjects.User;
using XFControls;
using XFControls.Utils;
using XFControls.XFPanels;
using XFControls.XFPanels.XFPanelItems;
using FBEntities.FBObjects;

namespace XFItemControls.XFPanels.XFPanelItems
{
    /// <summary>
    /// Icon for friends on the Home Screen
    /// </summary>
    public class XFIconFriend : IXFIcon
    {
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="person">Proxy for the person for whom the shortcut will be made</param>
        public XFIconFriend(FBProxyPerson person)
        {
            Icon = person.id;
            _person = new FBPerson { id = person.id, name = person.name };
            //if (XFGraphics.IsCached(person.Picture.url + "&type=large"))
            //    _imgUrl = person.Picture.url + "&type=large";
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="personId">Id for the person for whom the shortcut will be made</param>
        public XFIconFriend(string personId)
        {
            Icon = personId;
            var proxy = new FBProxyPerson(personId);
            _person = new FBPerson { id = personId, name = proxy.name };
            if (XFGraphics.IsCached(proxy.Picture.url + "&type=large"))
                _imgUrl = proxy.Picture.url + "&type=large";
        }

        /// <summary>
        /// Color of the Text
        /// </summary>
        public Color TextColor = Color.Black;
        /// <summary>
        /// Gets or sets the Homescreen index number.
        /// </summary>
        /// <value>
        /// The Homescreen index number.
        /// </value>
        public int Number { get; set; }
        /// <summary>
        /// Gets or sets the parent.
        /// </summary>
        /// <value>
        /// The parent.
        /// </value>
        public XFPanelIcon Parent { get; set; }
        /// <summary>
        /// Gets or sets the width.
        /// </summary>
        /// <value>
        /// The width.
        /// </value>
        public int Width { get; set; }
        /// <summary>
        /// Gets or sets the height.
        /// </summary>
        /// <value>
        /// The height.
        /// </value>
        public int Height { get; set; }
        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="XFIconFriend"/> is selected.
        /// </summary>
        /// <value>
        ///   <c>true</c> if selected; otherwise, <c>false</c>.
        /// </value>
        public bool Selected { get; set; }
        /// <summary>
        /// Gets or sets the image rectange.
        /// </summary>
        /// <value>
        /// The image rectange.
        /// </value>
        public Rectangle ImageRectange { get; set; }
        /// <summary>
        /// Gets or sets a value indicating whether this instance is empty.
        /// </summary>
        /// <value>
        ///   <c>true</c> if this instance is empty; otherwise, <c>false</c>.
        /// </value>
        public bool IsEmpty { get; set; }
        /// <summary>
        /// Gets the icon.
        /// </summary>
        public string Icon { get; private set; }

        private readonly string _imgUrl;
        private const int _padding = 5;
        private readonly FBPerson _person;

        /// <summary>
        /// Paints the icon.
        /// </summary>
        /// <param name="g">The graphics object.</param>
        /// <param name="x">The x coordinate.</param>
        /// <param name="y">The y coordinate.</param>
        public void PaintIcon(Graphics g, int x, int y)
        {
            using (var font = new Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular))
            {
                var textRec = new Rectangle(x + _padding, y, Width - (_padding << 1), 0);
                var textSize = font.MeasureString(_person.name, textRec);
                int iconWidth = Width - (_padding << 1);
                int iconHeight = Height - (_padding << 1) - textSize.Height;

                iconHeight = Math.Min(iconHeight, iconWidth);
                textRec.Y += iconHeight + _padding;
                textRec.Height = textSize.Height;

                ImageRectange = new Rectangle(x + ((Width >> 1) - (iconHeight >> 1)), y + _padding, iconHeight, iconHeight);

                g.DrawRoundedRectangleBorderImageFill(this,
                    !string.IsNullOrEmpty(_imgUrl) ? _imgUrl : "profile",
                    Color.FromArgb(120, 125, 128),
                    ImageRectange,
                    iconHeight >> 2,
                    3,
                    XFGraphicExtensions.RoundedCorners.All);

                g.DrawString(_person.name, textRec, XFItemTextAlign.Center, font, TextColor);
            }
        }

        /// <summary>
        /// Gets a Memberwise copy.
        /// </summary>
        /// <returns></returns>
        public IXFIcon GetCopy()
        {
            return MemberwiseClone() as IXFIcon;
        }

        /// <summary>
        /// Called when [click].
        /// </summary>
        public void OnClick()
        {
            XFPanelList rp = XFItemControlUtils.SetupProfile(_person);

            var back = new XFItemBackHeader();

            rp.AddHeaderItem(back);
            Parent.SetRightPanel(rp);

            if (Parent.Parent != null && Parent.Parent is XFPanelContainer)
                ((XFPanelContainer)Parent.Parent).SetHeaderLeftButton(new XFButtonBack());

            rp.ShowAlphaBar(false);
            rp.ShowScrollbar(true);

            Parent.EnableSlide();
        }
    }
}
